#!/usr/bin/env python
# encoding: utf-8
"""
TFIDF.py

Created by Pablo Ortega Mesa on 2011-04-05.
Copyright (c) 2011 Toeska Group. All rights reserved.
"""

import sys
import os
import getopt
import math
from scipy import linalg,array,dot,mat,transpose,log

class TFIDF():
	def __init__(self, docs, terms, TF):
		self.docs = docs
		self.terms = terms
		self.TF = TF
		self.TF_norm = self.TF_normalized()
		self.TF_relax = self.TF_relaxed()
		self.IDF = self.getIDF()
		self.score = self.calculateTFIDF()
		
	def __repr__(self):
		salida = ''
		for term in self.score:
			salida += '['
			for value in term:
				salida += str(value)+", "
			salida += "]\n"
		return salida
	
	def __getMaxFj__(self):
		''' Calculate maximus term-freq of each doc'''
		maxFj = []
		for j in xrange(0,len(self.docs)):
			fdoc = []
			for fterm in self.TF:
				fdoc.append(fterm[j])
			maxFj.append(max(fdoc))
		return maxFj
	
	def TF_normalized(self):
		maxFj = self.__getMaxFj__()
		''' 
		Now len(maxFj) and self.docs are size-equals
		Then, normalize the TF matrix
		'''
		TF_normalized = []
		for i in xrange(0,len(self.TF)):
			row = []
			for j in xrange(0,len(self.TF[i])):
				value = float(self.TF[i][j])/float(maxFj[j])
				row.append(value)
			TF_normalized.append(row)
		return TF_normalized
		
	def TF_relaxed(self):
		maxFj = self.__getMaxFj__()
		'''
		Now len(maxFj) and self.docs are size-equals
		Then, relax the TF matrix
		'''
		TF_relax = []
		for i in xrange(0,len(self.TF)):
			row = []
			for j in xrange(0,len(self.TF[i])):
				if self.TF[i][j]>0:
					value = float(1+math.log10(self.TF[i][j]))
				else:
					value = float(0)
				row.append(value)
			TF_relax.append(row)
		return TF_relax
	
	def getIDF(self):
		ni = []
		N = len(self.docs)
		for i in xrange(0,len(self.terms)):
			cont = 0
			for j in xrange(0,len(self.TF[i])):
				if self.TF[i][j] != 0:
					cont += 1
			if cont!=0:
				value = float(math.log10(float(N)/cont))
			else:
				print self.terms[i]
				value = float(0)
			ni.append(value)
		return ni
	
	
	def calculateTFIDF(self):
		matrix = []
		for i in xrange(0,len(self.terms)):
			row = []
			for j in xrange(0,len(self.docs)):
				value = float(self.TF_relax[i][j]*self.IDF[i])
				row.append(value)
			matrix.append(row)
		mmatrix = array(matrix)
		return matrix
	
	def invertirTFIDF(self,original):
		matrix = array(original)
		tmatrix = matrix.transpose()
		rows,cols = tmatrix.shape
		salida = []
		for r in xrange(0,rows):
			row = []
			for c in xrange(0,cols):
				row.append(tmatrix[r][c])
			salida.append(row)
		return salida
	def getTFIDF(self):
		return self.invertirTFIDF(self.score)
